package com.hottv.meeting.videomeeting.module.file.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.bumptech.glide.Glide;
import com.hottv.meeting.videomeeting.R;
import com.hottv.meeting.videomeeting.module.file.model.FileInfo;
import com.hottv.meeting.videomeeting.module.file.utils.FileUtil;
import com.hottv.meeting.videomeeting.module.white.DoCallback;

import java.util.List;


/**
 * 使用遍历文件夹的方式
 * Created by yis on 2018/4/17.
 */

public class FolderDataRecycleAdapter extends RecyclerView.Adapter<FolderDataRecycleAdapter.ViewHolder> {

    private Context mContext;
    private List<FileInfo> data;
    private boolean isPhoto = false;

    private DoCallback mCallback;


    public FolderDataRecycleAdapter(Context mContext, List<FileInfo> data, boolean isPhoto,DoCallback callback) {
        this.mContext = mContext;
        this.data = data;
        this.isPhoto = isPhoto;

        mCallback = callback;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_folder_data_rv_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {

        holder.tv_content.setText(data.get(position).getFileName());
        holder.iv_mask.setVisibility(View.INVISIBLE);
        holder.tv_size.setText(FileUtil.FormetFileSize(data.get(position).getFileSize()));
        holder.tv_time.setText(data.get(position).getTime());
        holder.itemView.setFocusable(true);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String filePath = data.get(position).getFilePath();
//                Log.i("FolderDataRecycle","filePath:"+data.get(position).getFilePath());
                if(mCallback!=null)
                {
                    Log.i("FolderDataRecycle","-----filePath:"+data.get(position).getFilePath());
                    mCallback.onSuccess(filePath);
                }
            }
        });

        holder.itemView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus)
                {
                    holder.iv_mask.setVisibility(View.VISIBLE);
                }else
                {
                    holder.iv_mask.setVisibility(View.INVISIBLE);
                }
            }
        });

        //封面图
        if (isPhoto) {
            Glide.with(mContext).load(data.get(position).getFilePath()).into(holder.iv_cover);
        } else {
            Glide.with(mContext).load(FileUtil.getFileTypeImageId(mContext, data.get(position).getFilePath())).fitCenter().into(holder.iv_cover);
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        RelativeLayout rlMain;
        TextView tv_content;
        TextView tv_size;
        TextView tv_time;
        ImageView iv_cover;

        ImageView iv_mask;

        public ViewHolder(View itemView) {
            super(itemView);
            rlMain = (RelativeLayout) itemView.findViewById(R.id.rl_main);
            tv_content = (TextView) itemView.findViewById(R.id.tv_content);
            tv_size = (TextView)itemView.findViewById(R.id.tv_size);
            tv_time = (TextView)itemView.findViewById(R.id.tv_time);
            iv_cover = (ImageView) itemView.findViewById(R.id.iv_cover);

            iv_mask = (ImageView) itemView.findViewById(R.id.iv_cover_mask);
        }
    }

}
